<?php

class ControllernewsCategoryNews extends Controller {

    public function index() {
        $this->load->model('tool/seo_url');
        $this->load->model('tool/custom');
        $this->language->load('news/category_news');
        //-------load model news and category_news--------------------------
        $this->load->model('catalog/category_news');
        $this->load->model('catalog/news');

        //------ get ID by seo_name of category ----------------------------
        if (isset($this->request->get['cat'])) {
            $category_news_id = $this->model_tool_custom->SeonameToID('category_news_description', $this->request->get['cat'], 'category_news_id');
        }

        $this->document->breadcrumbs = array();

        $this->data['breadcrumbs'][] = array(
            'href' => HTTP_SERVER,
            'text' => $this->language->get('text_home'),
            'separator' => FALSE
        );
        $this->data['breadcrumbs'][] = array(
                'href' => HTTP_SERVER.'news/',
                'text' => $this->language->get('text_news'),
                'separator' => $this->language->get('text_separator')
            );
        
        $category_info = $this->model_catalog_category_news->getCategory($category_news_id);

        if ($category_info) {

            $this->data['breadcrumbs'][] = array(
                'href' => HTTP_SERVER.'news/'.$category_info['seo_name'].'.html',
                'text' => $category_info['name'],
                'separator' => $this->language->get('text_separator')
            );

            $this->document->setTitle($category_info['name']);

            $this->document->setDescription( $category_info['meta_description']);

            $this->data['heading_title'] = $category_info['name'];

            $this->data['description'] = html_entity_decode($category_info['description'], ENT_QUOTES, 'UTF-8');

            $this->data['text_sort'] = $this->language->get('text_sort');

            //------------ pagination-------------------------------------------
            if (isset($this->request->get['page'])) {
                $page = $this->request->get['page'];

                $this->data['breadcrumbs'][] = array(
                    'href' => $this->model_tool_seo_url->rewrite($this->url->http('news/category_news&path_news=' . $path_news)) . '?page=' . $page,
                    'text' => $category_info['name'] . ' - ' . sprintf($this->language->get('text_page'), $page),
                    'separator' => $this->language->get('text_separator')
                );

                $this->document->setTitle($category_info['name'] . ' - ' . sprintf($this->language->get('text_page'), $page));

                $this->document->description = sprintf($this->language->get('text_page'), $page) . ' - ' . $category_info['meta_description'];
            } else {
                $page = 1;
            }

            $category_total = $this->model_catalog_category_news->getTotalCategoriesByCategoryId($category_news_id);
            $news_total = $this->model_catalog_news->getTotalnewsByCategoryId($category_news_id);

            if ($category_total || $news_total) {
                $results = $this->model_catalog_news->getnewssbyCatalog($category_news_id, ($page - 1) * 20, 20);

                $this->data['newss'] = array();

                foreach ($results as $result) {
                    $first_img = '';
                    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'), $matches);
                    if ($matches [1]) {
                        $first_img = $matches [1] [0];
                    } else {
                        $first_img = "image/no_image.jpg";
                    }

                    $this->data['newss'][] = array(
                        'item_id' => $result['news_id'],
                        'title' => $result['title'],
                        'date_added' => date('h:iA d/m/Y', strtotime($result['date_added'])),
                        'description' => $result['description'],
                        'image' => $first_img,
                        'href' => HTTP_SERVER.'news/'.$result['news_category'].'/'.$result['seo_name'].'.html'
                    );
                }

                $pagination = new Pagination();
                $pagination->total = $news_total;
                $pagination->page = $page;
                $pagination->limit = 20;
                $pagination->text = $this->language->get('text_pagination');
                $pagination->url = HTTP_SERVER.'news/'.$this->request->get['cat'].'.html?page=%s';

                $this->data['pagination'] = $pagination->render();


                if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/news/category_news.tpl')) {
                    $this->template = $this->config->get('config_template') . '/template/news/category_news.tpl';
                } else {
                    $this->template = 'default/template/news/category_news.tpl';
                }

                $this->children = array(
                    'common/header',
                    'common/footer',
                    'common/column_left',
                    'common/column_right'
                );

                $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
            } else {
                //-------------- if category is empty---------------------------
                $this->document->title = $category_info['name'];

                $this->document->description = $category_info['meta_description'];

                $this->data['heading_title'] = $category_info['name'];

                $this->data['text_error'] = $this->language->get('text_empty');

                $this->data['button_continue'] = $this->language->get('button_continue');

                $this->data['continue'] = $this->url->http('common/home');

                if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
                    $this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
                } else {
                    $this->template = 'default/template/error/not_found.tpl';
                }

                $this->children = array(
                    'common/header',
                    'common/footer',
                    'common/column_left',
                    'common/column_right'
                );

                $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
            }
        } else {
            $url = '';

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            if (isset($this->request->get['path_news'])) {
                $this->document->breadcrumbs[] = array(
                    'href' => $this->model_tool_seo_url->rewrite($this->url->http('news/category_news&path_news=' . $this->request->get['path_news'] . $url)),
                    'text' => $this->language->get('text_error'),
                    'separator' => $this->language->get('text_separator')
                );
            }

            $this->document->setTitle($this->language->get('text_error'));

            $this->data['heading_title'] = $this->language->get('text_error');

            $this->data['text_error'] = $this->language->get('text_error');

            $this->data['button_continue'] = $this->language->get('button_continue');

            $this->data['continue'] = $this->url->http('common/home');

            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
            } else {
                $this->template = 'default/template/error/not_found.tpl';
            }

            $this->children = array(
                'common/header',
                'common/footer',
                'common/column_left',
                'common/column_right'
            );

            $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
        }
    }

}

?>